package com.rainbow.bam.service.role;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rainbow.bam.dao.HtDutyRepository;
import com.rainbow.bam.dao.HtDutySrcActionRepository;
import com.rainbow.bam.entity.HtAccount;
import com.rainbow.bam.entity.HtDuty;
import com.rainbow.bam.entity.HtDutyAction;
import com.rainbow.bam.entity.HtSrcAction;
import com.rainbow.bam.entity.PaginationRequest;
import com.rainbow.bam.entity.PaginationResponse;
import com.rainbow.bam.util.Util;

/**
 * 角色服务实现类
 * 
 * @author zhaolei
 * @version $Id: HtDutyServiceImpl.java, v 0.1 2015年11月19日 下午1:15:04 zhaolei Exp $
 */
@Service
public class HtDutyServiceImpl implements HtDutyService {

    @Autowired
    private HtDutyRepository htDutyRepository;

    @Autowired
    private HtDutySrcActionRepository htDutySrcActionRepository;

    /**
     * 
     * @see com.rainbow.bam.service.role.HtDutyService#assignOperation(java.lang.Long, java.lang.Long[])
     */
    @Transactional
    public void assignOperation(final Long roleId, final Long[] operationIds) {
        HtDuty duty = (HtDuty) htDutyRepository.findOne(roleId);
        duty.getHtDutyActions().clear();
        this.htDutyRepository.merge(duty);
        for (int i = 0; i < operationIds.length; i++) {
            if (operationIds[i] == null) {
                continue;
            }
            HtDutyAction dutyAction = new HtDutyAction();
            dutyAction.setHtDuty(duty);
            HtSrcAction htSrcAction = new HtSrcAction();
            htSrcAction.setId(operationIds[i]);
            dutyAction.setHtSrcAction(htSrcAction);
            this.htDutySrcActionRepository.save(dutyAction);
        }
    }

    /**
     * 
     * @see com.rainbow.bam.service.role.HtDutyService#getAvailableRoles()
     */
    @SuppressWarnings("unchecked")
    public List<HtDuty> getAvailableRoles() {
        return htDutyRepository.findListByHql("select t from HtDuty t where t.status='A'");
    }

    /**
     * 
     * @see com.rainbow.bam.service.role.HtDutyService#getRoleById(java.lang.Long)
     */
    public HtDuty getRoleById(Long roleId) {
        HtDuty htDuty = htDutyRepository.findOne(roleId);
        return htDuty;
    }

    /**
     * 
     * @see com.rainbow.bam.service.role.HtDutyService#findDatasByIds(java.lang.String[])
     */
    @SuppressWarnings("unchecked")
    public List<HtDuty> findDatasByIds(String[] ids) {
        String param = Util.getIdSQLParam(ids);
        if (param != null) {
            return htDutyRepository.findListByHql("from HtDuty t where t.id in (" + param + ")");
        }
        return null;
    }

    /**
     * 
     * @see com.rainbow.bam.service.role.HtDutyService#deleteAll(java.util.List)
     */
    @Transactional
    public void deleteAll(List<HtDuty> list) {
        htDutyRepository.delete(list);
    }

    /**
     * 
     * @see com.rainbow.bam.service.role.HtDutyService#updateUser(java.util.List, java.lang.String)
     */
    public void updateUser(final List<HtAccount> userList, final String type) {
        //		// 如果类型为空退出
        //		if (type != null && type.trim().length() > 0) {
        //			// 循环用户对象列表
        //			for (FwAccount user : userList) {
        //				// 如果操作类型为重置密码
        //				if ("resetPwd".equals(type)) {
        //					// 设置用户密码为111111,并且用算法加密
        //					user.setAcctPwd(encryptServiceImpl.encrypt("111111"));
        //				} else if ("active".equals(type)) {
        //					// 设置用户状态为A
        //					user.setStatus("A");
        //				} else if ("inactive".equals(type)) {
        //					// 设置用户状态为D
        //					user.setStatus("D");
        //				}
        //				// 更新用户对象
        //				hibernateTemplate.update(user);
        //			}
        //		}
    }

    /**
     * 
     * @see com.rainbow.bam.service.role.HtDutyService#saveRole(com.rainbow.bam.entity.HtDuty)
     */
    @Override
    public void saveRole(HtDuty duty) {
        // TODO Auto-generated method stub
        this.htDutyRepository.save(duty);
    }

    /**
    * 
    * @see com.rainbow.bam.service.role.HtDutyService#findAccountsByPagination(com.rainbow.bam.entity.PaginationRequest)
    */
    @Override
    public PaginationResponse<HtDuty> findAccountsByPagination(PaginationRequest paginationRequest) {
        return htDutyRepository.findResultsByPagination(paginationRequest);
    }
}
